package com.example.fms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.fms.dto.resp.MenusDto;
import com.example.fms.dto.resp.RightRespDto;
import com.example.fms.entity.Right;
import com.example.fms.entity.RoleRights;
import com.example.fms.mapper.RightMapper;
import com.example.fms.mapper.RoleRightsMapper;
import com.example.fms.mapper.StandardFreightRuleMapper;
import com.example.fms.service.RightService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.Scanner;
@Service
@Slf4j
public class RightServiceImpl  extends ServiceImpl<RightMapper, Right> implements RightService {
    @Resource
    RightMapper rightMapper;

    @Resource
    RoleRightsMapper roleRightsMapper;
    @Override
    public List<RightRespDto> getRights(String type) {
        List<RightRespDto> rightRespDto = new ArrayList<>();
            List<Right> rights = rightMapper.selectList(null);
            if (!CollectionUtils.isEmpty(rights)) {
                rights.forEach(r -> {
                    RightRespDto dto = new RightRespDto();
                    dto.setId(r.getId());
                    dto.setLevel(r.getLevel());
                    dto.setPid(r.getPid());
                    dto.setAuthName(r.getAuthName());
                    dto.setPath(r.getPath());
                    rightRespDto.add(dto);
                });
            }
        return rightRespDto;
    }

    @Override
    public List<MenusDto> getMenus(Long rid,Boolean isAdmin) {
        List<Right> rights = List.of();
        List<Long> rightsIdList = List.of();
        if (rid == -1) {
            rights = rightMapper.selectList(null);
        } else {
            LambdaQueryWrapper<RoleRights> qw = new LambdaQueryWrapper<>();
            qw.eq(RoleRights::getRole, rid);
            List<RoleRights> roleRights = roleRightsMapper.selectList(qw);
            if (roleRights != null && !roleRights.isEmpty()) {
                rightsIdList  = roleRights.stream().map(RoleRights::getRightsId).collect(Collectors.toList());
                rights = rightMapper.selectList(new LambdaQueryWrapper<Right>().in(Right::getId, rightsIdList));
            }
        }

        Map<Long, List<Right>> rightMap = rights.stream().collect(Collectors.groupingBy(Right::getPid));
        // 初始化一级菜单列表
        List<MenusDto> rootMenus = new ArrayList<>();
        for (Right auth : rights) {
            if (auth.getPid() == null || auth.getPid() == 0) {
                MenusDto menusDto = new MenusDto();
                menusDto.setRids(rightsIdList);
                // 如果PID为0或null，则为一级菜单
                menusDto.setPid(auth.getPid());
                menusDto.setId(auth.getId());
                menusDto.setAuthName(auth.getAuthName());
                menusDto.setPath(auth.getPath());
                menusDto.setLevel(auth.getLevel());
                menusDto.setChildren(buildMenuTree(rightMap, auth.getId()));
                rootMenus.add(menusDto);
            }
        }
        return rootMenus;
    }
    private List<MenusDto> buildMenuTree(Map<Long, List<Right>> authMap, Long pid) {
        List<MenusDto> children = new ArrayList<>();
        List<Right> auths = authMap.getOrDefault(pid, new ArrayList<>());
        for (Right auth : auths) {
            MenusDto menusDto = new MenusDto();
            menusDto.setPid(auth.getPid());
            menusDto.setId(auth.getId());
            menusDto.setAuthName(auth.getAuthName());
            menusDto.setPath(auth.getPath());
            menusDto.setLevel(auth.getLevel()); // 将Auth对象属性复制到MenusDto对象
            menusDto.setChildren(buildMenuTree(authMap, auth.getId()));
            children.add(menusDto);
        }
        return children;
    }


}
